const App = getApp();
// 默认未登录的灰色头像
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

  data: {
    // 页面标题
    page: {},

    // 页面元素
    items: {},
    // 商户详情
    detail:{},
    // 答题详情
    zimuArr:['A','B','C','D','E','F'],
    qaObj:{},
    allQestionArr:[],
    // 推广页面制作
    user_id:0,
    trueMobile: "",
    avatarUrl: defaultAvatarUrl,
    useShopDes: "no", //是否使用店铺详情作为本页面信息展示
    inline_fen: 0, //0积分，表示不开启页面推广
    shop_id:0, //推广对应的商户id
    send_uid:0, //地推人员id号
    send_qaid:0, //答题送积分，试卷id号
    now_allFen:0, //获取用户最新的积分，只有直接送积分的时候才获取，做试卷不获取，也是初始状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    // 页面id
    _this.data.page_id = options.page_id;
    // 加载页面数据
    _this.getPageData();
    // 设置navbar标题、颜色
    App.setNavigationBar();
    this.setData({trueMobile:wx.getStorageSync('true_mobile')});
    if(options.send_uid>0){
      this.setData({send_uid:options.send_uid});
    }
  },
  onShow(){
    App.pageOnLoad(this);
  },
  /**
   * 加载页面数据
   */
  getPageData: function(callback) {
    let _this = this;
    App._get('page/index', {
      page_id: _this.data.page_id
    }, function(result) {
      // 设置顶部导航栏栏
      _this.setPageBar(result.data.page);
      _this.setData(result.data);
      _this.setData({useShopDes:result.data.page.params.useShopDes=='yes' ? result.data.page.params.useShopDes : 'no'});
      // 扫描推广码才携带这些参数，直接转发不携带这些参数
      if(result.data.page.params.shop_id>0 && _this.data.send_uid>0){
        console.log('推广数据结果：',result.data.page.params.shop_id,result.data.page.params.inline_fen)
        _this.setData({shop_id:result.data.page.params.shop_id, 
          send_qaid:result.data.page.params.send_qaid,
          inline_fen:result.data.page.params.inline_fen});
        _this.getShopDec();
        _this.getQaDec();
      }
      // 回调函数
      typeof callback === 'function' && callback();
    });
  },
  /** 活动无图文内容则展示店铺相亲 */
  getShopDec(shop_id){
    let _this = this;
    App._get('shop/detail', { shop_id:_this.data.shop_id }, function(result) {
      _this.setData({detail:result.data.detail});
    });
  },
  /** 活动无图文内容则展示问答详情 */
  getQaDec(shop_id){
    let _this = this;
    var send_qaid = this.data.send_qaid;
    if(send_qaid>0){
      App._get('askanswer/getAllQestion', {qaid:send_qaid}, result => {
        _this.setData({allQestionArr:result.data.list, qaObj:result.data.qaObj});
        console.log(result);
      });
    }
  },
  /**
   * 设置顶部导航栏
   */
  setPageBar: function(page) {
    // 设置页面标题
    wx.setNavigationBarTitle({
      title: page.params.title
    });
    // 设置navbar标题、颜色
    wx.setNavigationBarColor({
      frontColor: page.style.titleTextColor === 'white' ? '#ffffff' : '#000000',
      backgroundColor: page.style.titleBackgroundColor
    })
  },
  // 点击获取手机号
  getUserMobile(e){
    let _this = this
    console.log("获取用户手机号", e)
    if(e.detail.iv && 1){
      App._post_form('user/getwxMobile', {
        user_id: wx.getStorageSync('user_id'),
        encryptedData: e.detail.encryptedData,
        code: e.detail.code,
        iv: e.detail.iv,
      }, function(result) {
        console.log("获取到微信手机号码------", result)
        let nowMobile = result.data.true_mobile
        if(nowMobile!=''){
          wx.setStorageSync('true_mobile', result.data.true_mobile);
          _this.setData({trueMobile:result.data.true_mobile,user_id: wx.getStorageSync('user_id')})
          //如果得到了手机号，并且上次暂存的分享来源是yes，则展示领取试题的按钮
          _this.getFen()
        }
      }, false, function() {
        wx.hideLoading();
        //_this.onClosePopup();
      })
    }
  },
  /** 真正的获取积分函数 */
  getFen(){
    var _this = this;
    App._post_form('ditui/getFen', {
      page_id: _this.data.page_id,
      page_title: _this.data.page.params.share_title,
      shop_id: _this.data.shop_id,
      send_qaid:_this.data.send_qaid,
      send_uid:_this.data.send_uid,
      inline_fen: _this.data.inline_fen,
      user_id: wx.getStorageSync('user_id'),
    }, function(result) {
      var dd = result.data.back;
      //如果直接领取积分领取成功
      if(dd.state=='getJfOk'){
        _this.setData({now_allFen:dd.newPoints});
        App.showSuccess(dd.msg);
      }
      //如果问答领取成功
      if(dd.state=='getQaOk'){
        var buyid = dd.buyid;
        wx.redirectTo({
          url: '/pages/studycenter/beganswer?fromShare=yes&buyid=' + buyid,
        });
      }
    })
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function() {
    // 获取首页数据
    this.getPageData(function() {
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage: function() {
    const _this = this;
    // 构建页面参数
    const params = App.getShareUrlParams({
      'page_id': _this.data.page_id
    });
    return {
      title: _this.data.page.params.share_title,
      path: "/pages/custom/index?" + params
    };
  },

  /**
   * 分享到朋友圈
   * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
   * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
   */
  onShareTimeline: function() {
    const _this = this;
    // 构建页面参数
    const params = App.getShareUrlParams({
      'page_id': _this.data.page_id
    });
    return {
      title: _this.data.page.params.share_title,
      path: "/pages/custom/index?" + params
    };
  },

});